package com.ymatou.query.seller.trading.api.parameter;

import java.util.List;
import java.util.Map;

import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtCreditDetailWapper;
import com.ymttest.database.sqlwapper.YmtOOrderNoteWapper;
import com.ymttest.database.sqlwapper.YmtOrderExtWapper;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.database.sqlwapper.YmtTradingItemWapper;

/**
 * 存储过程：sp_trd_GetSingleOrderInfo_1
 * 
 * @author songyefeng
 *
 */
@SuppressWarnings("rawtypes")
public class Sp_GetSingleOrderInfo_1 {
	private Integer userId;
	private Integer orderId;

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	public void execute() {
		YmtOrdersWapper ordersWapper = new YmtOrdersWapper();
		DefaultWapper defaultWapper = new DefaultWapper();
		if (userId != null && userId == -1) {
			userId = (Integer) ordersWapper
					.selectOrders2("iUserId", null, "iOrderId = " + orderId)
					.get(0).get("iUserId");
		}
		StringBuffer field = new StringBuffer();
		field.append("o.[iOrderId],");
		field.append("  o.[fSellerPromotionAmount],");
		field.append("  o.[iUserId],");
		field.append("  o.[iBuyerId],");
		field.append("  o.[sMarkId],");
		field.append("  o.[dAddTime],");
		field.append("  o.[fOrderPrice],");
		field.append("  o.[fOrderDiscount],");
		field.append("  o.[fFreight],");
		field.append("  o.[fDiscount],");
		field.append("  o.[iTradingId],");
		field.append("  o.[iOperate],");
		field.append("  o.[dOperateExpireTime],");
		field.append("  o.[sAddress],");
		field.append("  o.[sPostCode],");
		field.append("  o.[sReceivePerson],");
		field.append("  o.[sPhone],");
		field.append("  o.[sTelephone],");
		field.append("  o.[sQQ],");
		field.append("  o.[sEmail],");
		field.append("  o.[sLeaveWord],");
		field.append("  o.[iUnfreezeStatus],");
		field.append("  o.[dDispathTime],");
		field.append("  o.[iTopicId],");
		field.append("  o.[sTitle],");
		field.append("  o.[iReplyTopicWhenOrderPaid],");
		field.append("  o.[bPaidInFull],");
		field.append("  o.[fUseGiftAmount],");
		field.append("  o.[sCouponCode],");
		field.append("  o.[CouponValue],");
		field.append("  o.[iCouponType],");
		field.append("  o.[dPaidTime],");
		field.append("  o.[dApplyPostPayTime],");
		field.append("  o.[dPostPaidTime],");
		field.append("  o.[dConfirmedTime],");
		field.append("  o.[dChangeAddressTime],");
		field.append("  o.[iDistributor],");
		field.append("  o.[sThirdOrderId],");
		field.append("  o.[iType],");
		field.append("  o.[fOldFreight],");
		field.append("  o.[dDiscountTime],");
		field.append("  o.[fOldDiscount],");
		field.append("  o.[fAutoCancelOrderHours],");
		field.append("  o.[dCancelTime],");
		field.append("  o.[bShangouOrder],");
		field.append("  o.[sBuyerLoginId],");
		field.append("  o.[sBuyerLoginEmail],");
		field.append("  o.[sSellerLoginId],");
		field.append("  o.[sSellerLoginEmail],");
		field.append("  o.[iIsMerchant],");
		//		field.append("  o.[sBuyerNickName],");
		field.append("  u.[sNickName] as sBuyerNickName,");
		field.append("  o.[fTotalPrice],");
		field.append("  o.[fUseFreeCardAmount],");
		field.append("  o.[sHostRef],");
		field.append("  o.[bIncludeActivityProducts],");
		field.append("  o.[bShippedByXlobo],");
		field.append("  o.[dAcceptTime],");
		field.append("  o.[sCurType],");
		field.append("  o.[bCanLocalReturn],");
		field.append("  o.[bApplyLocalReturn],");
		field.append("  o.[dApplyLocalReturnTime],");
		field.append("  o.[iRiskVerifiedStatus],");
		field.append("  o.[dLastUpdateTime],");
		field.append("  o.[iThirdPartyRefundStatus],");
		field.append("  o.[iCouponChannel],");
		field.append("  o.[iSalesRefundStatus],");
		field.append("  o.[bDomesticDelivered],");
		field.append("  CASE WHEN r.iProcessStatus IS NULL THEN o.iTradingStatus WHEN r.iProcessStatus = 0 THEN 3 ELSE o.iTradingStatus END AS iTradingStatus,");
		field.append("  (SELECT TOP 1 sReason  FROM Ymt_OrderReason( NOLOCK ) WHERE iOrderId = "
				+ orderId + ")  AS sReason,");
		field.append(" o.[sYmtCouponCode],");
		field.append("	 o.[sSellerCouponCode],");
		field.append("   o.[fYmtCouponAmount],");
		field.append("   o.[fSellerCouponAmount],");
		field.append("   o.[fPayableAmount],");
		field.append("   o.[iMainOrderId],");
		field.append("   o.[bPreSale],");
		field.append("   o.[bCanEvaluate],");
		field.append("   [RealPay].*,");
		field.append("   [BusynessTypes] = (select cast(BusynessType as varchar(10)) + ',' from [dbo].[Ymt_OrderFrozenDetail] as [frozen] where [frozen].[OrderId]=o.[iOrderId] and [frozen].[IsFrozen]=1 for xml path('') )");
		StringBuffer join = new StringBuffer();
		join.append(" LEFT JOIN Temp_ReceivedOrders20151109( NOLOCK ) r ON o.iOrderId = r.iOrderId");
		join.append(" LEFT JOIN ymt_users( NOLOCK ) u ON o.iUserId = u.iUserId");
		join.append(" outer apply (");
		join.append(" select [RealSettlementAmountOfCoupon] = sum(case [t3].[SalesRefundStatus] when 10 then [SettlementAmountOfCoupon] else 0 end ), [TotalSalesRefundAmount] = sum(t3.[RefundAmount]) from [dbo].[Ymt_RefundBill] as t3 with(nolock) where t3.[OrderId] = o.[iOrderId]");
		join.append(" ) as [RealPay]");
		String queryCondition = "o.iOrderId = " + orderId
				+ " AND (o.iUserId = " + userId + " OR o.iBuyerId = " + userId
				+ ")";
		orders = ordersWapper.selectOrders2(field.toString(), join.toString(),
				queryCondition);

		if (orders.size() == 0) {
			return;
		}

		String field2 = "iOrderId,iOrderType,sOrderSource,bIsNeedUploadIdCard,bHaveUploadedIdCard,bNewSellerOrder,bNewCustomerOrder,bIsFirstOrder";
		orderExts = new YmtOrderExtWapper().selectOrderExt(field2, null,
				"iOrderId = " + orderId);

		// creditDetails = new YmtCreditDetailWapper().selectCreditDetail("*",
		// null,
		// "stargetid = '" + orderId + "'");
		// ymt_creditdetail表作废了
		creditDetails = new YmtCreditDetailWapper().selectCreditDetail("*",
				null, "1=0");

		notes = new YmtOOrderNoteWapper().selectOrderNotes(
				"iOrderId,sContent,iRemarkLevel", null, "iuserid = " + userId
						+ " AND iorderid = " + orderId);

		states = defaultWapper.selectOrderStatesByOrderIds("*", null,
				"iorderid = " + orderId);

		postPays = defaultWapper.selectOrderPostPay("*", null, "iorderid = "
				+ orderId);

		String sqlInfo = "SELECT "
				+ " i.*, "
				+ " e.sOrderInfoId AS sOrderInfoExtId, "
				+ " e.iActivityId,"
				+ " e.iActivityTemplateId,"
				+ " r.RefundBillNo,"
				+ " s.[PromotionId] , s.[PromotionType] , s.[PromotionName] , s.[MatchCondition] , s.[PromotionValue],s.[ReducedAmount]"
				+ " FROM Ymt_OrderInfo as i with( NOLOCK )"
				+ " LEFT JOIN Ymt_OrderInfoExt as e with(nolock) on i.sOrderInfoId = e.sOrderInfoId"
				+ " LEFT JOIN Ymt_RefundProduct as r with(nolock) on i.sOrderInfoId = r.OrderInfoId"
				+ " left join [dbo].[Ymt_SellerPromotion] as s with(nolock) on i.[iOrderId] = s.[OrderId] and i.[sOrderInfoId] = s.[OrderInfoId]"
				+ " WHERE i.iOrderId = " + orderId;
		infos = defaultWapper.executeSql(sqlInfo);

		// 获取订单物流信息
		summarys = defaultWapper.selectOrderSummary2(
				"iOrderId,iBillType,sSummary", null, "iorderid = " + orderId);

		// 获取订单冻结信息
		orderFrozens = defaultWapper.selectOrderFrozen("*", null, "iorderid = "
				+ orderId);

		YmtTradingItemWapper tradingItemWapper = new YmtTradingItemWapper();

		tradingItem = new DefaultWapper()
				.executeSql(
						"select isnull(iTradingId, iMainOrderId) as [iTradingId],iOrderId from ymt_orders o where o.iOrderId="
								+ orderId).get(0);

		orderToBills = defaultWapper
				.executeSql(" SELECT * FROM Ymt_OrderToBill( NOLOCK ) WHERE 1=0");

		bills = defaultWapper
				.executeSql(" SELECT * FROM Ymt_Bill( NOLOCK ) where 1=0");

	}

	private List<Map> orders;
	private List<Map> orderExts;
	private List<Map> creditDetails;
	private List<Map> notes;
	private List<Map> states;
	private List<Map> postPays;
	private List<Map> infos;
	private List<Map> summarys;
	private List<Map> orderFrozens;
	private Map tradingItem;
	private List<Map> orderToBills;
	private List<Map> bills;

	/**
	 * Ymt_Orders
	 * 
	 * @return
	 */
	public List<Map> getOrders() {
		return orders;
	}

	/**
	 * Ymt_OrderExt
	 * 
	 * @return
	 */
	public List<Map> getOrderExts() {
		return orderExts;
	}

	/**
	 * Ymt_CreditDetail
	 * 
	 * @return
	 */
	public List<Map> getCreditDetails() {
		return creditDetails;
	}

	/**
	 * Ymt_O_OrderNote
	 * 
	 * @return
	 */
	public List<Map> getNotes() {
		return notes;
	}

	/**
	 * Ymt_OrderState
	 * 
	 * @return
	 */
	public List<Map> getStates() {
		return states;
	}

	/**
	 * Ymt_OrderPostPay
	 * 
	 * @return
	 */
	public List<Map> getPostPays() {
		return postPays;
	}

	/**
	 * Ymt_OrderInfo
	 * 
	 * @return
	 */
	public List<Map> getInfos() {
		return infos;
	}

	/**
	 * Ymt_OrderSummary
	 * 
	 * @return
	 */
	public List<Map> getSummarys() {
		return summarys;
	}

	/**
	 * Ymt_Order_Frozen
	 */
	public List<Map> getOrderFrozens() {
		return orderFrozens;
	}

	/**
	 * Ymt_TradingItem
	 * 
	 * @return
	 */
	public Map getTradingItem() {
		return tradingItem;
	}

	/**
	 * Ymt_OrderToBill
	 * 
	 * @return
	 */
	public List<Map> getOrderToBills() {
		return orderToBills;
	}

	/**
	 * Ymt_Bill
	 * 
	 * @return
	 */
	public List<Map> getBills() {
		return bills;
	}

	public static void main(String[] args) {
		Sp_GetSingleOrderInfo_1 sp = new Sp_GetSingleOrderInfo_1();
		sp.setOrderId(112464361);
		sp.setUserId(20338042);
		sp.execute();
		System.out.println(sp.getBills());
	}

}
